package com.foreveross.fetl.core.domain.freight.hbase.pre;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.OneToMany;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;

import com.foreveross.fetl.common.InstanceFactory;
import com.foreveross.fetl.common.domain.BaseHBaseEntity;
import com.foreveross.fetl.common.jpa.annontation.HBaseColumn;
import com.foreveross.fetl.core.domain.freight.hbase.AgentEntity;
import com.foreveross.fetl.core.domain.freight.hbase.CabinEntity;
import com.foreveross.fetl.core.domain.freight.hbase.PriceEntity;
import com.foreveross.fetl.core.domain.freight.hbase.ReturnTripEntity;
import com.foreveross.fetl.core.domain.freight.hbase.TransitchangeEntity;
import com.foreveross.fetl.core.domain.freight.repository.IPlaneInfoRepository;
/**
 * hbase预处理即时表
 * @author xiangsf 2013-10-31s
 *
 */
@javax.persistence.Table(name="LP_FLIGHT")
public class LpFreightEntity extends BaseHBaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5919357642872560002L;
	/**
	 * 运营商简称英文，如：MU
	 */
	@HBaseColumn(qualifier="carrierKey",family="planeinfo")
	private String carrierKey;

	/**
	 * 运营商简称中文，如：南航
	 */
	@HBaseColumn(qualifier="carrierName",family="planeinfo")
	private String carrierName;

	/**
	 * 运营商全名，如：中国海南航空公司
	 */
	@HBaseColumn(qualifier="carrierFullName",family="planeinfo")
	private String carrierFullName;

	/**
	 * 航班批次日期。如:2012-05-19,格式:yyyy-MM-dd
	 */
	@HBaseColumn(qualifier="flightBatchDate",family="planeinfo")
	private String flightDate;

	/**
	 * 航班起飞时间
	 */
	@HBaseColumn(qualifier="flightBatchStartTime",family="planeinfo", format="yyyyMMddHHmmss")
	private Date startTime;

	/**
	 * 航班预计到达时间
	 */
	@HBaseColumn(qualifier="flightBatchEndTime",family="planeinfo", format="yyyyMMddHHmmss")
	private Date endTime;

	/**
	 * 航班号，如： MU3540
	 */
	@HBaseColumn(qualifier="flightId",family="planeinfo")
	private String flightNo;

	/**
	 * 目的地三字码
	 */
	@HBaseColumn(qualifier="toCity",family="planeinfo")
	private String toCity;
	/**
	 * 目的地名称
	 */
	@HBaseColumn(qualifier="toCityName",family="planeinfo")
	private String toCityName;
	/**
	 * 区域码
	 */
	@HBaseColumn(qualifier="areaCode",family="planeinfo")
	private String areaCode;
	/**
	 * 区域名称
	 */
	@HBaseColumn(qualifier="areaName",family="planeinfo")
	private String areaName;
	/**
	 * 起飞地三字码
	 * 
	 */
	@HBaseColumn(qualifier="fromCity",family="planeinfo")
	private String fromCity;
	/**
	 * 起飞地
	 * 
	 */
	@HBaseColumn(qualifier="fromCityName",family="planeinfo")
	private String fromCityName;

	/**
	 * 最低价格
	 */
	@HBaseColumn(qualifier="lowerPrice",family="planeinfo")
	private Double lowerPrice;

	/**
	 * 最低价代理商名称
	 */
	@HBaseColumn(qualifier="agentName",family="planeinfo")
	private String agentName;

	/**
	 * 最高价格
	 */
	@HBaseColumn(qualifier="highPrice",family="planeinfo")
	private Double highPrice;
	/**
	 * 附加的hbase应用值，用于生成rowkey,以便于应用根据该值统计
	 * <p>实际上使用下面的"抓取渠道ID"也可，但基于应用已完成部分功能，补充上该值
	 */
	@HBaseColumn(qualifier="grabChannelNum",family="planeinfo")
	private String attachHbaseKey;
	/**
	 * 抓取渠道ID
	 * */
	@HBaseColumn(qualifier="grabChannelId",family="planeinfo")
	private Long grabChannelId;
	/**
	 * 抓取渠道名称
	 */
	@HBaseColumn(qualifier="grabChannel",family="planeinfo")
	private String grabChannel;
	/**
	 * 外键，对应表CRAWL_PF_SOURCE_DATA的rowkey
	 * 
	 */
	@HBaseColumn(qualifier="SOURCE_ROWKEY",family="planeinfo")
	private String sourceRowkey;
	
	/**
	 * 飞机类型
	 */
	@HBaseColumn(qualifier="type",family="planeinfo")
	private String type = "";
	/**
	 * 创建时间
	 */
	 @HBaseColumn(qualifier="grabDate",family="planeinfo", format="yyyyMMddHHmmssSSS")
	private Date createTime = new Date();
	 /**
	 * 中转
	 */
	@OneToMany
	@HBaseColumn(qualifier="transit",family="planeinfo")
	private List<TransitchangeEntity> transit = new ArrayList<TransitchangeEntity>();
	
	public List<TransitchangeEntity> getTransit() {
		return transit;
	}

	public void setTransit(List<TransitchangeEntity> transit) {
		this.transit = transit;
	}

	/**
	 * 代理商信息
	 */
	@OneToMany
	@HBaseColumn(qualifier="agents",family="agentInfos")
	private Set<AgentEntity> agents = new HashSet<AgentEntity>();
	/**
	 * 仓位信息
	 */
	@OneToMany
	@HBaseColumn(qualifier="cabins",family="planeinfo")
	private Set<CabinEntity> cabins = new HashSet<CabinEntity>();
	/**
	 * 价格除了仓位外，还分多个种类，比如燃油费，附加税费
	 */
	@OneToMany
	@HBaseColumn(qualifier="prices",family="planeinfo")
	private Set<PriceEntity> prices = new HashSet<PriceEntity>();
	/**
	 * 航班运价的类型，0为单程价，1为往返程价, 2为联程
	 */
	@HBaseColumn(qualifier="freightType",family="planeinfo")
	private Integer freightType;
	/**
	 * 往返程价
	 */
	@HBaseColumn(qualifier="roundTripPrice",family="planeinfo")
	private Double roundTripPrice;
	/**
	 * 联程价
	 */
	@HBaseColumn(qualifier="roundTripPrice",family="planeinfo")
	private Double interlinePrice;
	/**
	 * 回程实体信息
	 * 一个航班可能会有多种回程，同一天的不同时间段
	 */
	@OneToMany
	@HBaseColumn(qualifier="returnTrips",family="planeinfo")
	private Set<ReturnTripEntity> returnTrips = new HashSet<ReturnTripEntity>();;
	
	public LpFreightEntity(){}
	
	public String getAttachHbaseKey() {
		return attachHbaseKey;
	}

	public void setAttachHbaseKey(String attachHbaseKey) {
		this.attachHbaseKey = attachHbaseKey;
	}

	/**
	 * 生成航班数据的hbase主键 20002HGHXMN00SC49742013101413
	 * 渠道hbasekey_起点(使用三字码)_终点(使用三字码)_航班ID_航班日期_小时
     *	5位 +    		3位   +         3位      + 	    8位+  10位 + 2位
	 */
	 
	@Override
	public String generateRowKey() {
		return new StringBuilder()
				.append(StringUtils.leftPad(String.valueOf(this.getAttachHbaseKey()), 5, '0'))
				.append(this.getFromCity())
				.append(this.getToCity())
				.append(StringUtils.leftPad(this.getFlightNo(), 8, '0'))
				.append(DateFormatUtils.format(this.getStartTime(), "yyyyMMddHH"))
				.toString();
	}

	public IPlaneInfoRepository getPlaneInfoRepository() {
		return InstanceFactory.getInstance(IPlaneInfoRepository.class);
	}
	
	public String getCarrierKey() {
		return carrierKey;
	}
	public void setCarrierKey(String carrierKey) {
		this.carrierKey = carrierKey;
	}
	public String getCarrierName() {
		return carrierName;
	}
	public void setCarrierName(String carrierName) {
		this.carrierName = carrierName;
	}
	public String getCarrierFullName() {
		return carrierFullName;
	}
	public void setCarrierFullName(String carrierFullName) {
		this.carrierFullName = carrierFullName;
	}
	public String getFlightDate() {
		return flightDate;
	}
	public void setFlightDate(String flightDate) {
		this.flightDate = flightDate;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String getFlightNo() {
		return flightNo;
	}
	public void setFlightNo(String flightNo) {
		this.flightNo = flightNo;
	}
	public String getToCity() {
		return toCity;
	}
	public void setToCity(String toCity) {
		this.toCity = toCity;
	}
	public String getToCityName() {
		return toCityName;
	}
	
	public String getSourceRowkey() {
		return sourceRowkey;
	}

	public void setSourceRowkey(String sourceRowkey) {
		this.sourceRowkey = sourceRowkey;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}
	public String getFromCity() {
		return fromCity;
	}
	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}
	public String getFromCityName() {
		return fromCityName;
	}
	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}
	public Double getLowerPrice() {
		return lowerPrice;
	}
	public void setLowerPrice(Double lowerPrice) {
		this.lowerPrice = lowerPrice;
	}
	public String getAgentName() {
		return agentName;
	}
	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}
	public Double getHighPrice() {
		return highPrice;
	}
	public void setHighPrice(Double highPrice) {
		this.highPrice = highPrice;
	}
	public Long getGrabChannelId() {
		return grabChannelId;
	}
	public void setGrabChannelId(Long grabChannelId) {
		this.grabChannelId = grabChannelId;
	}
	public String getGrabChannel() {
		return grabChannel;
	}
	public void setGrabChannel(String grabChannel) {
		this.grabChannel = grabChannel;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Set<AgentEntity> getAgents() {
		return agents;
	}
	public void setAgents(Set<AgentEntity> agents) {
		this.agents = agents;
	}
	public Set<CabinEntity> getCabins() {
		return cabins;
	}
	public void setCabins(Set<CabinEntity> cabins) {
		this.cabins = cabins;
	}
	public Set<PriceEntity> getPrices() {
		return prices;
	}
	public void setPrices(Set<PriceEntity> prices) {
		this.prices = prices;
	}
	
	public String getAreaCode() {
		return areaCode;
	}
	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}
	public String getAreaName() {
		return areaName;
	}
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	
	public Integer getFreightType() {
		return freightType;
	}

	public void setFreightType(Integer freightType) {
		this.freightType = freightType;
	}

	public Double getRoundTripPrice() {
		return roundTripPrice;
	}

	public void setRoundTripPrice(Double roundTripPrice) {
		this.roundTripPrice = roundTripPrice;
	}

	public Double getInterlinePrice() {
		return interlinePrice;
	}

	public void setInterlinePrice(Double interlinePrice) {
		this.interlinePrice = interlinePrice;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("PlaneInfoEntity [carrierKey=");
		builder.append(carrierKey);
		builder.append(", carrierName=");
		builder.append(carrierName);
		builder.append(", carrierFullName=");
		builder.append(carrierFullName);
		builder.append(", flightDate=");
		builder.append(flightDate);
		builder.append(", startTime=");
		builder.append(startTime);
		builder.append(", endTime=");
		builder.append(endTime);
		builder.append(", flightNo=");
		builder.append(flightNo);
		builder.append(", toCity=");
		builder.append(toCity);
		builder.append(", toCityName=");
		builder.append(toCityName);
		builder.append(", areaCode=");
		builder.append(areaCode);
		builder.append(", areaName=");
		builder.append(areaName);
		builder.append(", fromCity=");
		builder.append(fromCity);
		builder.append(", fromCityName=");
		builder.append(fromCityName);
		builder.append(", lowerPrice=");
		builder.append(lowerPrice);
		builder.append(", agentName=");
		builder.append(agentName);
		builder.append(", highPrice=");
		builder.append(highPrice);
		builder.append(", grabChannelId=");
		builder.append(grabChannelId);
		builder.append(", grabChannel=");
		builder.append(grabChannel);
		builder.append(", type=");
		builder.append(type);
		builder.append(", createTime=");
		builder.append(createTime);
		builder.append(", agents=");
		builder.append(agents);
		builder.append(", cabins=");
		builder.append(cabins);
		builder.append(", transit=");
		builder.append(transit);
		builder.append(", prices=");
		builder.append(prices);
		builder.append("]");
		return builder.toString();
	}

	public Set<ReturnTripEntity> getReturnTrips() {
		return returnTrips;
	}

	public void setReturnTrips(Set<ReturnTripEntity> returnTrips) {
		this.returnTrips = returnTrips;
	}
}
